import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 防止路由重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  // 默认加载界面
  {
    path: '/',
    redirect: '/login'
  },
  // 登录页面界面
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  // 注册页面路由
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue')
  },
  {
    path:'/aaa',
    name: 'aaa',
    component: () => import('@/views/Home/aaa.vue')
  },
  {
    path:'/bbb',
    name: 'bbb',
    component: () => import('@/views/Home/bbb.vue')
  },
  {
    path:'/ccc',
    name: 'ccc',
    component: () => import('@/views/Home/ccc.vue')
  },
  // 总路由
  {
    path: '/manager',
    name: 'manager',
    component: () => import('@/views/Manager.vue'),
    children:[
      // 配置子路由的默认加载页面
      {
        path:'/manager',
        redirect:'/manager/home'
      },
      // 首页路由
      {
        path:'home',
        component: () => import('@/views/Home/Home.vue')
      },
      // 订单路由
      {
        path: 'order',
        component: () => import('@/views/Order/Order.vue')
      },
      // 帮助路由
      {
        path: 'Income',
        component: () => import('@/views/Income/Income.vue')
      },
      // 我的页面路由
      {
        path: 'mine',
        component: () => import('@/views/Mine/Mine.vue')
      },
      // 产品页面的路由
      {
        path:'product',
        component: () => import('@/views/Product/product.vue')
      },
      // 订单详情的路由
      {
        path: 'onorder',
        component: () => import('@/views/Order/onorder.vue')
      },
       // 待接单单详情的路由
       {
        path: 'onorder1',
        component: () => import('@/views/Order/onorder1.vue')
      },
      // 待服务单详情的路由
      {
        path: 'onorder2',
        component: () => import('@/views/Order/onorder2.vue')
      },
      {
        path: 'userconfirm',
        component: () => import('@/views/Mine/UserConfirm.vue')
      },
      {
        path: 'userinfo',
        component: () => import('@/views/Mine/userinfo.vue')
      },
        // 已完成详情的路由
      {
        path: 'onorder3',
        component: () => import('@/views/Order/onorder3.vue')
      },
      // 已完成评论跳转
      {
        path: 'comment',
        component: () => import('@/views/Order/comment.vue')
      },
    ]
  },
  
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫 判断有无token
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  //若要跳转的页面是登录界面
  if (to.path === '/login') {
    //直接跳转
    next();
  } else { //若想要跳转其他页面
    //获取本地存储的token值
    let token = localStorage.getItem('token');
    //若token为空则验证不成功，跳转到登录页面
    if (token === 'null' || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export default router
